001 /*
002 * Copyright 2005 Stephen J. McConnell
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
013 * implied.
014 *
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019 package net.dpml.tools.tasks;
020
021 import java.io.File;
022 import java.io.FileOutputStream;
023
024 import net.dpml.library.Resource;
025 import net.dpml.library.Module;
026 import net.dpml.library.info.Scope;
027 import net.dpml.library.info.ModuleDirective;
028 import net.dpml.library.info.LibraryEncoder;
029
030 import net.dpml.tools.Context;
031
032 import org.apache.tools.ant.Project;
033 import org.apache.tools.ant.BuildException;
034
035 /**
036 * Write a module to XML in a form suitable for publication.
037 *
038 * @author <a href="http://www.dpml.net">The Digital Product Meta Library</a>
039 * @version 1.0.0
040 */
041 public class ModuleTask extends GenericTask
042 {
043 /**
044 * Task initialization.
045 * @exception BuildException if a build error occurs.
046 */
047 public void init() throws BuildException
048 {
049 if( !isInitialized() )
050 {
051 super.init();
052 getContext().getPath( Scope.TEST );
053 }
054 }
055
056 /**
057 * Execute the task.
058 */
059 public void execute()
060 {
061 Project project = getProject();
062 final Context context = getContext();
063 final String path = context.getLayoutFilename( "module" );
064 final File deliverables = context.getTargetDeliverablesDirectory();
065 final File modules = new File( deliverables, "modules" );
066 final File module = new File( modules, path );
067 writeModuleFile( module );
068 }
069
070 private void writeModuleFile( final File file )
071 {
072 Resource resource = getResource();
073 if( resource instanceof Module )
074 {
075 Module module = (Module) resource;
076 ModuleDirective directive = (ModuleDirective) module.export();
077 writeModuleDirective( directive, file );
078 }
079 else
080 {
081 final String error =
082 "Project is not a module.";
083 throw new BuildException( error, getLocation() );
084 }
085 }
086
087 private void writeModuleDirective( ModuleDirective directive, File file )
088 {
089 FileOutputStream output = null;
090 try
091 {
092 log( "Exporting module to: " + file );
093 final LibraryEncoder encoder = new LibraryEncoder();
094 file.getParentFile().mkdirs();
095 output = new FileOutputStream( file );
096 encoder.export( directive, output );
097 }
098 catch( Exception e )
099 {
100 final String error =
101 "Failed to export module.";
102 throw new BuildException( error, e, getLocation() );
103 }
104 finally
105 {
106 if( null != output )
107 {
108 try
109 {
110 output.close();
111 }
112 catch( Exception ioe )
113 {
114 }
115 }
116 }
117 }
118 }